function Drawer(workspace){
    this.workspace = workspace;
    this.objects = new Array();
}

Drawer.prototype = {
    constructor: Drawer,
    draw : function(obj){

        if(obj == null){
            return this._drawAll();
        }else if(obj instanceof Point){
            return this._drawPoint(obj);
        }else if(obj instanceof Line){
            return this._drawLine(obj);
        }else if(obj instanceof Polyline){
            return this._drawPolyline(obj);
        }else if(obj instanceof Polygon){
            return this._drawPolyline(obj);
        }else if(obj instanceof Rectangle){
            return this._drawRectangle(obj);
        }else if(obj instanceof Circle){
            return this._drawCircle(obj);
        }else if(obj instanceof Ellipse){
            return this._drawEllipse(obj);
        }else if(obj instanceof Path){
            return this._drawPath(obj);
        }
        return null;
    },
    addObject: function(obj, options){
        this.objects.push(obj);

        if(obj instanceof Point){
            obj.svgElement = this._createElement("circle");
        }else if(obj instanceof Line){
            obj.svgElement = this._createElement("line");
        }else if(obj instanceof Polyline){
            obj.svgElement = this._createElement("polyline");
        }else if(obj instanceof Polygon){
            obj.svgElement = this._createElement("polygon");
        }else if(obj instanceof Rectangle){
            obj.svgElement = this._createElement("rect");
        }else if(obj instanceof Circle){
            obj.svgElement = this._createElement("circle");
        }else if(obj instanceof Ellipse){
            obj.svgElement = this._createElement("ellipse");
        }else if(obj instanceof Path){
            obj.svgElement = this._createElement("path");
        }
        if(!obj.svgElement){
            return null;
        }
        obj.svgOptions = options != null ? options : {};
        return obj;
    },
    removeObject: function(obj){
        for(var i = 0; i < this.objects.length ; i++){
            if(obj === this.objects[i]){
                this.objects.splice(i,1);
                delete obj["svgElement"];
                delete obj["svgOptions"];
                return obj;
            }
        }
        return null;
    },
    removeAllObjects: function(){
        //TODO: voir pou faire mieux car les attribut svgMachin reste sur l'objet
        this.objects = new Array();
    },
    _drawLine: function(line){
        this._setElementAttributes(line.svgElement, {
                "x1": line.start.x,
                "y1": line.start.y,
                "x2": line.end.x,
                "y2": line.end.y,
                "stroke-width": 1,
                "stroke": "black"
        });
        this._setElementAttributes(line.svgElement, line.svgOptions);
        this.workspace.appendChild(line.svgElement);
        return line;
    },
    _drawPolyline: function(polyline){
        var pointsStringBuilder = new Array();
        for(var i = 0 ; i < polyline.points.length ; i++){
            pointsStringBuilder.push(polyline.points[i].x+","+polyline.points[i].y);
        }
        var pointsString = pointsStringBuilder.join(" ");
        this._setElementAttributes(polyline.svgElement, {
            "points": pointsString,
            "stroke-width": 1,
            "stroke": "black",
            "fill": "transparent"
        });
        this._setElementAttributes(polyline.svgElement, polyline.svgOptions);
        this.workspace.appendChild(polyline.svgElement);
        return polyline;
    },
    _drawPoint: function(point){
        this._setElementAttributes(point.svgElement, {
            "cx": point.x,
            "cy": point.y,
            "r": 3
        });
        this._setElementAttributes(point.svgElement, point.svgOptions);
        this.workspace.appendChild(point.svgElement);
        return point;
    },
    _drawRectangle: function(rectangle){
        this._setElementAttributes(rectangle.svgElement, {
            "x": rectangle.origin.x,
            "y": rectangle.origin.y,
            "width": rectangle.width,
            "height": rectangle.height,
            "stroke-width": 1,
            "stroke": "black",
            "fill": "transparent"
        });
        this._setElementAttributes(rectangle.svgElement, rectangle.svgOptions);
        this.workspace.appendChild(rectangle.svgElement);
        return rectangle;
    },
    _drawCircle: function(circle){
        this._setElementAttributes(circle.svgElement, {
            "cx": circle.origin.x,
            "cy": circle.origin.y,
            "r": circle.radius,
            "stroke-width": 1,
            "stroke": "black",
            "fill": "transparent"
        });
        this._setElementAttributes(circle.svgElement, circle.svgOptions);
        this.workspace.appendChild(circle.svgElement);
        return circle;
    },
    _drawEllipse: function(ellipse){
        this._setElementAttributes(ellipse.svgElement, {
            "cx": ellipse.origin.x,
            "cy": ellipse.origin.y,
            "rx": ellipse.xRadius,
            "ry": ellipse.yRadius,
            "stroke-width": 1,
            "stroke": "black",
            "fill": "transparent"
        });
        this._setElementAttributes(ellipse.svgElement, ellipse.svgOptions);
        this.workspace.appendChild(ellipse.svgElement);
        return ellipse;
    },
    _drawPath: function(path){
        this._setElementAttributes(path.svgElement, {
            "d": path.path,
            "stroke-width": 1,
            "stroke": "black",
            "fill": "transparent"
        });
        this._setElementAttributes(path.svgElement, path.svgOptions);
        this.workspace.appendChild(path.svgElement);
        return path;
    },
    _drawAll: function(){
        for(var i = 0 ; i < this.objects.length ; i++){
            this.draw(this.objects[i]);
        }
        return this.objects.length > 0;
    },
    clearAll: function(){
        if ( this.workspace.hasChildNodes() ){
            while ( this.workspace.childNodes.length >= 1 ){
                this.workspace.removeChild( this.workspace.firstChild );
            }
        }
        return this.objects.length > 0;
    },
    clear: function(obj){
        if(obj.svgElement){
            return this.workspace.removeChild(obj.svgElement);
        }
        return null;
    },
    _createElement:function(type, attributes){
        var svgElement = document.createElementNS("http://www.w3.org/2000/svg" , type);
        this._setElementAttributes(svgElement, attributes);
        return svgElement;
    },
    _setElementAttributes:function (element, attributes){
        for(var attribute in attributes){
            var value = attributes[attribute];
            element.setAttributeNS(null, attribute, value);
        }
    },
    getObjectAttribute:function (obj, attribute){
        return obj.svgOptions[attribute];
    },
    setObjectAttribute:function (obj, attribute, value){
        obj.svgOptions[attribute] =  value;
    },
    setObjectAttributes:function (obj, attributes){
        for(var attribute in attributes){
            this.setObjectAttribute(obj, attribute, attributes[attribute]);
        }
    }





}